package jdbc;

import com.google.protobuf.Enum;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 将教语文的老师工资涨500
 * 测试事务的提交与回滚
 */
public class Test {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();){
            connection.setAutoCommit(false);
            String  sql = "UPDATE teacher SET salary = salary - 500 WHERE subject_id=(SELECT id FROM subject WHERE name ='语文')";

            Statement statement = connection.createStatement();
            int num = statement.executeUpdate(sql);
            System.out.println(num>0? "更新成功":"更新失败");
//            connection.commit();//提交事务
//            System.out.println("事务提交了!");

            connection.rollback();//回滚事务
            System.out.println("事务回滚了!");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
